package cn.org.timespace.easyframe.action.permission;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;

import cn.org.timespace.easyframe.action.EFBaseAction;
import cn.org.timespace.easyframe.model.permission.EfOrg;
import cn.org.timespace.easyframe.model.permission.EfSystemHelp;
import cn.org.timespace.easyframe.model.permission.SystemMessage;
import cn.org.timespace.easyframe.service.permission.EfOrgService;
import cn.org.timespace.easyframe.service.permission.SystemMessageService;
import cn.org.timespace.easyframe.util.chart.ChartUtil;
import cn.org.timespace.easyframe.util.generator.IdentityGenerator;

/**
 * 系统公告
 * 
 * @author wubo
 * 
 */
public class SystemMessageAction extends EFBaseAction {

	private static final long serialVersionUID = 5583015541887048157L;
	
	private SystemMessageService systemMessageService;
	private EfOrgService efOrgService;

	public SystemMessageAction() {
		super(new SystemMessageModel());
	}

	private SystemMessageModel getSystemMessageModel() {
		return (SystemMessageModel) this.getModel();
	}

	public String doList() throws Exception {
		SystemMessageModel systemMessageModel = getSystemMessageModel();
		List<SystemMessage> systemMessages = systemMessageService.findByCondition(systemMessageModel);
		getRequest().setAttribute(VOLIST, systemMessages);

		return LIST;
	}

	/**
	 * 保存系统公告
	 * 
	 * @return
	 * @throws Exception
	 */
	public String doSave() throws Exception {

		String smId = IdentityGenerator.getInstance().getId();
		SystemMessage systemMessage = new SystemMessage();
		systemMessage = getSystemMessageModel().getSystemMessage();
		systemMessage.setSmId(smId);
		systemMessage.setOperatorUser(getEfUserSession().getEfUser().getUserName());
		systemMessage.setOperatorDate(getCurDate());
		systemMessage.setEfOrg(getEfUserSession().getEfUser().getEfOrg());
		systemMessageService.save(systemMessage);

		return doList();
	}

	public String doFindById() {
		String smId = getRequest().getParameter("smId");
		if (!StringUtils.isEmpty(smId)) {
			SystemMessage systemMessage = systemMessageService.findById(smId);
			getSystemMessageModel().setSystemMessage(systemMessage);
			return UPDATE;
		}
		List<EfOrg> efOrgs = efOrgService.findAll();
		getRequest().setAttribute("efOrgs", efOrgs);
		return ADD;
	}
	
	/**
	 * 查看系统公告
	 * 
	 * @return
	 * @throws Exception
	 */
	public String doView() throws Exception {

		String smId = getRequest().getParameter("smId");
		SystemMessage systemMessage = systemMessageService.findById(smId);
		getSystemMessageModel().setSystemMessage(systemMessage);
		
		return VIEW;
	}

	public String doUpdate() throws Exception {
		SystemMessage systemMessage = getSystemMessageModel().getSystemMessage();
		systemMessage.setEditUser(getEfUserSession().getEfUser().getUserName());
		systemMessage.setEditDate(getCurDate());
		systemMessage.setEfOrg(getEfUserSession().getEfUser().getEfOrg());
		systemMessageService.update(systemMessage);
		return doList();
	}

	public String doDelete() throws Exception {
		String smId = getRequest().getParameter("smId");
		SystemMessage systemMessage = systemMessageService.findById(smId);
		systemMessageService.delete(systemMessage);
		return doList();
	}

	public EfOrgService getEfOrgService() {
		return efOrgService;
	}

	public void setEfOrgService(EfOrgService efOrgService) {
		this.efOrgService = efOrgService;
	}

	public SystemMessageService getSystemMessageService() {
		return systemMessageService;
	}

	public void setSystemMessageService(
			SystemMessageService systemMessageService) {
		this.systemMessageService = systemMessageService;
	}
	
	/**
	 * 保存系统帮助
	 * @return
	 * @throws Exception
	 */
	public String doSaveEfSystemHelp() throws Exception {
		String pageMsg = "保存成功";
		
		try{
		
		SystemMessageModel systemMessageModel = getSystemMessageModel();
		EfSystemHelp efSystemHelp = systemMessageModel.getEfSystemHelp();
		efSystemHelp.setModifyUserName(getEfUserSession().getEfUser().getUserName());
		efSystemHelp.setModifyDate(getCurDate());
		systemMessageService.updateEfSystemHelp(efSystemHelp);
		
		}catch(Exception e){
			pageMsg = "保存失败！原因是："+e.getMessage();
		}
		
		getRequest().setAttribute(PAGEMSG,pageMsg);
		return "systemHelpEdit";
		
	}
	
	/**
	 * 查看系统帮助
	 * @return
	 * @throws Exception
	 */
	public String doViewEfSystemHelp() throws Exception {
		EfSystemHelp efSystemHelp = systemMessageService.findSysteHelp();
		getSystemMessageModel().setEfSystemHelp(efSystemHelp);
		return "systemHelpView";
	}
	
	public String demo3DPie() throws Exception {
		Map map=new HashMap();
		map.put("中国", "1");
		map.put("美国", "2");
		map.put("欧洲", "3");
		String fileName=ChartUtil.generate3DPieChart(getRequest(), map,"全球经济调查报道",500,300);
		getRequest().setAttribute("filename",fileName);
		return "demo3DPie";
	}
	
	public String demoBarChart() throws Exception {
		
		String[] rowKeys = {"苹果", "梨子", "葡萄"};    
		String[] columnKeys = {"北京", "上海", "广州", "成都", "深圳"};
		double[][] data = new double[][] { {672, 766, 223, 540, 126}, {325, 521,    
            210, 340, 106}, {332, 256, 523, 240, 526}};    
		String fileName=ChartUtil.generateBarChart(getRequest(),rowKeys,columnKeys,data,"中国水果市场调查",500,300);
		getRequest().setAttribute("filename",fileName);
		return "demoBarChart";
	}
	
    public String demoLineChart() throws Exception {
    	
    	Map map=new HashMap();
		map.put("1", "100");
		map.put("2", "200");
		map.put("3", "200");
		
		Map map1=new HashMap();
		map1.put("1", "150");
		map1.put("2", "250");
		map1.put("3", "250");
		
    	Map map2=new HashMap();
		map2.put("短信",map);
		map2.put("彩信",map1);
		String fileName=ChartUtil.generateDayLine(getRequest(),"信息发送量",map2);
		getRequest().setAttribute("filename",fileName);
		return "demoLineChart";
	}
    
    public String demoDashboardChart() throws Exception {
    
		String fileName=ChartUtil.generateDashboard(getRequest(),"衡阳铁一中考勤统计","刷卡量",22D);
		getRequest().setAttribute("filename",fileName);
		return "demoDashboardChart";
	}
}
